/**
 * 
 */
package com.taobao.snowsnow.taobao;

import java.sql.Timestamp;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

import com.taobao.api.ApiException;
import com.taobao.api.request.ItemGetRequest;
import com.taobao.api.request.ItemsOnsaleGetRequest;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemsOnsaleGetResponse;
import com.taobao.snowsnow.hibernate.pojo.ProductCollect;
import com.taobao.snowsnow.util.GB2Alpha;

/**
 * <p>统计SKU为0的商品, 如果SKU为了,说明在淘宝网上没有显示颜色分类, 这样买家想买也不能买了
 * @author tux
 *
 */
public class ProductInfo  extends Taobao {
	
	public ProductInfo() throws Exception {
		getPage();
	}
	
	public ProductInfo(long num_iid) throws ApiException, DocumentException {
		getItemInfo(num_iid);
	}
	
	/**
	 * 获取一个分页的商品
	 * @throws Exception 
	 */
	private void getPage() throws Exception {
		ItemsOnsaleGetRequest req = new ItemsOnsaleGetRequest();
		
//			ItemsGetRequest req = new ItemsGetRequest();

		// 获取 num_iid, 用来获取商品详情
		req.setFields("num_iid");

		// 淘宝id名称
//			req.setNicks("myleft1");

		// 每页大小
		req.setPageSize(40l);
		
		for(long i = 1; i <= 40l; i++) {
			// 页码
			req.setPageNo(i);
			
			// sessionKey
			ItemsOnsaleGetResponse response = client.execute(req, TaoBaoUtil.SESSIONKEY);

			getItems(response.getBody());
		}
		System.out.println("over");
	}

	/**
	 * 获取多个商品
	 * @param xmlBody
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public void getItems(String xmlBody) throws Exception {
		Document document = DocumentHelper.parseText(xmlBody);

		List<DefaultElement> list = document.selectNodes("//items_onsale_get_response/items/item");
		
		for (DefaultElement df : list) {
			
			Element num_iid = df.element("num_iid");
			
			long iid = Long.valueOf(num_iid.getText());
//					System.out.println(iid);

			getItemInfo(iid);
//			System.out.println(i++);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void getItemInfo(long num_iid) throws ApiException, DocumentException {
		ItemGetRequest req1 = new ItemGetRequest();
		req1.setFields("num_iid, created, title, price, detail_url, pic_url, property_alias, sku");
		req1.setNumIid(num_iid);

		ItemGetResponse response = client.execute(req1, TaoBaoUtil.SESSIONKEY);
		Document doc = DocumentHelper.parseText(response.getBody());
		List<DefaultElement> l = doc.selectNodes("//item_get_response/item");
		
		
		for (DefaultElement d : l) {
			
			List<DefaultElement> sku = doc.selectNodes("//skus/sku");
			int ss = sku.size();
			
			if(ss == 0)
				System.out.println("http://item.taobao.com/item.htm?id=" + num_iid);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
//			new ProductInfo(9892240992l);
			new ProductInfo();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
